<?php

/**
 * alltosun.com 商品接口 user.php
 * ============================================================================
 * 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址: http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 张元昊 (zhangyh@alltosun.com) $
 * $Date: 2015-11-30 14:42:16 $
 * $Id$
 */
class Action
{

    private $per_page = 50;

    private $token = '';
    private $user_id = 0;
    private $user_info = array();
    private $api_stat_id = 0;

    public function __construct() {
        $this->token = an_request('token');

        if ($this->token) {
            $user_info = user_helper::get_user_info_by_token($this->token);
            if ($user_info && $user_info['status'] == 1) {
                $this->user_id = $user_info['id'];
                $this->user_info = $user_info;
            }
        }
    }

    /**
     * 获取店铺商品列表
     * 接口有两处调用，调价和掌柜进货
     * get_channel_store_goods_list
     */
    public function get_store_goods_list() {
        $page = an_request('page_no');
        $store_id = an_request('store_id');
        $app_version = an_request('v', 0);

        $sign_param = array(
            'page_no' => $page,
            'store_id' => $store_id
        );

        $per_num = 70;
        if ($app_version > 1.6) {
            $per_num = 10;
        }
        if (!$page) {
            $page = 1;
        }

        // 验证接口
        $this->api_stat_id = api_helper::check_sign($sign_param, 1);

        if (!$this->user_id) {
            api_helper::return_data(1, '该用户不存在或已禁用');
        }

        if (!$store_id) {
            api_helper::return_data(1, '无效的商铺ID');
        }

        $store_info = _uri('store', $store_id);
        if (!$store_info || $store_info['status'] == 0) {
            api_helper::return_data(1, '商铺不存在或者被禁用');
        }

        if ($store_info['user_id'] != $this->user_id &&
            $store_info['owner_id'] != $this->user_id &&
            $store_info['agency_id'] != $this->user_id &&
            $store_info['marketing_id'] != $this->user_id
        ) {
            api_helper::return_data(1, '商铺与用户不对应');
        }

        // 查询合伙人信息
        $agency_store_info = _uri('store', array('user_id' => $store_info['agency_id']));

        // 获取所属合伙人商品
        $store_filter = array('store_id' => $agency_store_info['id'], 'status' => 1);
        $store_goods_ids = _model('store_goods')->getFields('goods_id', $store_filter);

        // 获取总店信息
        $channel_list = array();
        $user_store_info = _uri('user_store', array('user_id' => $store_info['owner_id']));
        if ($user_store_info) {
            // 门店渠道信息
            $channel_info = channel_helper::get_channel_info_by_type($user_store_info['type']);
            if ($channel_info) {
                $channel_list = channel_helper::get_store_channel_list_by_id($channel_info['id']);
            }
        }

        // 获取合伙人总店信息
        $dealers_user_store_info = store_helper::get_user_store($store_info['agency_id']);

        // 这个接口掌柜调用不用返回终端建议价，防止抓包泄露
        $is_show_market_price = false;
        if ($this->user_info['role_type'] != user_config::STORE_MANAGER &&
            $this->user_info['role_type'] != user_config::STORE_OWNER
        ) {
            $is_show_market_price = true;
        }

        // 按照渠道优先
        $channel_goods_ids = array();
        if ($channel_list) {
            foreach ($channel_list as $channel_goods_info) {
                $channel_goods_ids[] = $channel_goods_info['goods_id'];
            }
        }

        // 上架并且未删掉的
        $filter = array('status' => 1, 'is_sale' => '1');
        if ($channel_goods_ids) {
            $filter['id'] = $channel_goods_ids;
        }

        // 有可能存在后台下架的商品，合伙人还有库存，也要展示出来
        $data_list = get_app_data_list('goods', $filter, 'ORDER BY `view_order` DESC', $page, $per_num);
        if (!$data_list) {
            api_helper::return_data(1, '暂无商品数据!');
        }
        if ($data_list) {

            $page_arr = $data_list['page'];
            $goods_list = $data_list['data'];

            foreach ($goods_list as $k => $v) {

                if ($v['cover']) {
                    $cover_path = _uri('attachment', $v['cover'], 'path');
                    $goods_list[$k]['cover'] = _image($cover_path, 'list');
                }

                $goods_list[$k]['category_name'] = _uri('category', $v['category_id'], 'title');
                $sku_list = goods_helper::get_store_sku_list($store_info['id'], $v['id'], $user_store_info['addon_price']);

                $info['id'] = $goods_list[$k]['id'];
                $info['title'] = $goods_list[$k]['title'];
                $info['intro'] = $goods_list[$k]['intro'];
                $info['view_order'] = $goods_list[$k]['view_order'];
                $info['is_sale'] = 1;
                $info['category_name'] = $goods_list[$k]['category_name'];
                $info['category_id'] = $goods_list[$k]['category_id'];
                $info['cover'] = $goods_list[$k]['cover'];
                $info['unit'] = $goods_list[$k]['unit'];

                if ($is_show_market_price) {
                    $info['market_price'] = $goods_list[$k]['market_price'];
                }

                $sku_format_list = array();
                if ($sku_list) {
                    foreach ($sku_list as $key => $val) {
                        $sku_format_list[$key]['id'] = $val['id'];
                        $sku_format_list[$key]['user_id'] = $val['user_id'];
                        $sku_format_list[$key]['sku_id'] = $val['sku_id'];
                        $sku_format_list[$key]['store_id'] = $val['store_id'];
                        $sku_format_list[$key]['goods_id'] = $val['goods_id'];
                        $sku_format_list[$key]['price'] = $val['price'];
                        // TODO bbcoin 向上取整 终端暂不支持兑换 规则待商议
                        // 2016-08-09 @蒙
                        $sku_format_list[$key]['bbcoin'] = ceil($val['price']);

                        if ($is_show_market_price) {
                            $sku_format_list[$key]['market_price'] = $val['market_price'];
                            if ($val['market_price'] > 0) {
                                $info['market_price'] = $val['market_price'];
                            }
                        }
                    }
                }

                $info['sku_list'] = $sku_format_list;

                // 缺少sku直接表示缺货
                if (!$sku_format_list) {
                    $info['is_sale'] = 0;
                    //                     $info['sku_list'][0]['id'] = 0;
                    //                     $info['sku_list'][0]['user_id'] = $store_info['user_id'];
                    //                     $info['sku_list'][0]['sku_id'] = 0;
                    //                     $info['sku_list'][0]['store_id'] = $store_info['id'];
                    //                     $info['sku_list'][0]['goods_id'] = $v['id'];
                    //                     $info['sku_list'][0]['market_price'] = (string)$v['market_price'];
                    //                     if ($is_show_market_price) $info['sku_list'][0]['market_price'] = (string)$v['market_price'];
                }

                // 获取合伙人店铺的库存
                $agency_sku_list = store_helper::get_store_sku_info($dealers_user_store_info['id'], $v['id']);
                if ($agency_sku_list) {
                    foreach ($agency_sku_list as $key => $val) {
                        // 合伙人改价忽略上架下架问题，门店进货要有下架的问题
                        if ($val['stock'] <= 0 && !$is_show_market_price) {
                            $info['is_sale'] = 0;
                        }
                    }
                } else {
                    $info['is_sale'] = 0;
                }

                $result[] = $info;
            }
        }

        $result_data = $result;
        if ($app_version > 1.6) {
            $result_data = array();
            $result_data['data_list'] = $result;
            $result_data['pages'] = $page_arr;
        }

        api_helper::return_data(0, 'success', $result_data, $this->api_stat_id);
    }

    /**
     * 获取店铺商品列表
     * @wangdk 2015-5-6 弃用
     */
    public function get_store_goods_list2() {
        $page = an_request('page_no');
        $store_id = an_request('store_id');

        $sign_param = array(
            'page_no' => $page,
            'store_id' => $store_id
        );

        // 验证接口
        $this->api_stat_id = api_helper::check_sign($sign_param, 1);

        if (!$this->user_id) {
            api_helper::return_data(1, '该用户不存在或已禁用');
        }

        if (!$store_id) {
            api_helper::return_data(1, '无效的商铺ID');
        }

        $store_info = _uri('store', $store_id);
        if (!$store_info || $store_info['status'] == 0) {
            api_helper::return_data(1, '商铺不存在或者被禁用');
        }

        if ($store_info['user_id'] != $this->user_id &&
            $store_info['owner_id'] != $this->user_id &&
            $store_info['agency_id'] != $this->user_id &&
            $store_info['marketing_id'] != $this->user_id
        ) {
            api_helper::return_data(1, '商铺与用户不对应');
        }

        // 查询合伙人信息
        $agency_store_info = _uri('store', array('user_id' => $store_info['agency_id']));

        // 获取所属合伙人商品
        $store_filter = array('store_id' => $agency_store_info['id'], 'status' => 1);
        $store_goods_ids = _model('store_goods')->getFields('goods_id', $store_filter);

        // 获取总店信息
        $user_store_info = _uri('user_store', array('user_id' => $store_info['owner_id']));

        // 获取合伙人总店信息
        $dealers_user_store_info = store_helper::get_user_store($store_info['agency_id']);

        // 这个接口掌柜调用不用返回终端建议价，防止抓包泄露
        $is_show_market_price = false;
        if ($this->user_info['role_type'] != user_config::STORE_MANAGER &&
            $this->user_info['role_type'] != user_config::STORE_OWNER
        ) {
            $is_show_market_price = true;
        }

        // 上架并且未删掉的
        $filter = array('status' => 1, 'is_sale' => '1');

        // 有可能存在后台下架的商品，合伙人还有库存，也要展示出来
        $goods_list = get_data_list('goods', $filter, 'ORDER BY `view_order` DESC', $page, 50);
        if ($goods_list) {

            foreach ($goods_list as $k => $v) {

                if ($v['cover']) {
                    $cover_path = _uri('attachment', $v['cover'], 'path');
                    $goods_list[$k]['cover'] = _image($cover_path, 'list');
                }

                $goods_list[$k]['category_name'] = _uri('category', $v['category_id'], 'title');
                $sku_list = goods_helper::get_store_sku_list($store_info['id'], $v['id'], $user_store_info['addon_price']);

                $info['id'] = $goods_list[$k]['id'];
                $info['title'] = $goods_list[$k]['title'];
                $info['intro'] = $goods_list[$k]['intro'];
                $info['view_order'] = $goods_list[$k]['view_order'];
                $info['is_sale'] = 1;
                $info['category_name'] = $goods_list[$k]['category_name'];
                $info['category_id'] = $goods_list[$k]['category_id'];
                $info['cover'] = $goods_list[$k]['cover'];

                if ($is_show_market_price) {
                    $info['market_price'] = (string)$goods_list[$k]['market_price'];
                }

                if ($sku_list) {
                    foreach ($sku_list as $key => $val) {
                        $sku_format_list[$key]['id'] = $val['id'];
                        $sku_format_list[$key]['user_id'] = $val['user_id'];
                        $sku_format_list[$key]['sku_id'] = $val['sku_id'];
                        $sku_format_list[$key]['store_id'] = $val['store_id'];
                        $sku_format_list[$key]['goods_id'] = $val['goods_id'];
                        $sku_format_list[$key]['price'] = (string)$val['price'];

                        if ($is_show_market_price) {
                            $sku_format_list[$key]['market_price'] = (string)$val['market_price'];
                            if ($val['market_price'] != '0.0') {
                                $info['market_price'] = (string)$val['market_price'];
                            }
                        }
                    }
                }

                $info['sku_list'] = $sku_format_list;


                // 获取合伙人店铺的库存
                $agency_sku_list = store_helper::get_store_sku_info($dealers_user_store_info['id'], $v['id']);
                if ($agency_sku_list) {
                    foreach ($agency_sku_list as $key => $val) {
                        // 合伙人改价忽略上架下架问题，门店进货要有下架的问题
                        if ($val['stock'] <= 0 && !$is_show_market_price) {
                            $info['is_sale'] = 0;
                        }
                    }
                } else {
                    $info['is_sale'] = 0;
                }

                $result[] = $info;
            }
        }

        api_helper::return_data(0, 'success', $result, $this->api_stat_id);
    }

    /**
     * 获取店铺商品列表
     * 最早一版本 弃用 @wangdk 2016-5-6
     */
    public function get_store_goods_list3() {
        $page = an_request('page_no');
        $store_id = an_request('store_id');

        $sign_param = array(
            'page_no' => $page,
            'store_id' => $store_id
        );

        // 验证接口
        $this->api_stat_id = api_helper::check_sign($sign_param, 1);

        if (!$this->user_id) {
            api_helper::return_data(1, '该用户不存在或已禁用');
        }

        if (!$store_id) {
            api_helper::return_data(1, '无效的商铺ID');
        }

        $store_info = _uri('store', $store_id);
        if (!$store_info || $store_info['status'] == 0) {
            api_helper::return_data(1, '商铺不存在或者被禁用');
        }

        if ($store_info['user_id'] != $this->user_id &&
            $store_info['owner_id'] != $this->user_id &&
            $store_info['agency_id'] != $this->user_id &&
            $store_info['marketing_id'] != $this->user_id
        ) {
            api_helper::return_data(1, '商铺与用户不对应');
        }

        // 查询合伙人信息
        $agency_store_info = _uri('store', array('user_id' => $store_info['agency_id']));

        // 获取所属合伙人商品
        $store_filter = array('store_id' => $agency_store_info['id'], 'status' => 1);
        $store_goods_ids = _model('store_goods')->getFields('goods_id', $store_filter);

        // 获取总店信息
        $user_store_info = _uri('user_store', array('user_id' => $store_info['owner_id']));

        // 获取合伙人总店信息
        $dealers_user_store_info = store_helper::get_user_store($store_info['agency_id']);

        // 这个接口掌柜调用不用返回终端建议价，防止抓包泄露
        $is_show_market_price = false;
        if ($this->user_info['role_type'] != user_config::STORE_MANAGER &&
            $this->user_info['role_type'] != user_config::STORE_OWNER
        ) {
            $is_show_market_price = true;
        }

        if ($store_goods_ids) {
            $goods_list = _model('goods')->getList($store_goods_ids, 'ORDER BY `view_order` DESC');
            foreach ($goods_list as $k => $v) {

                if ($v['cover']) {
                    $cover_path = _uri('attachment', $v['cover'], 'path');
                    $goods_list[$k]['cover'] = _image($cover_path, 'list');
                }

                $goods_list[$k]['category_name'] = _uri('category', $v['category_id'], 'title');
                $sku_list = goods_helper::get_store_sku_list($store_info['id'], $v['id'], $user_store_info['addon_price']);

                $info['id'] = $goods_list[$k]['id'];
                $info['title'] = $goods_list[$k]['title'];
                $info['intro'] = $goods_list[$k]['intro'];
                $info['view_order'] = $goods_list[$k]['view_order'];
                $info['is_sale'] = 1;
                $info['category_name'] = $goods_list[$k]['category_name'];
                $info['category_id'] = $goods_list[$k]['category_id'];
                $info['cover'] = $goods_list[$k]['cover'];

                if ($is_show_market_price) {
                    $info['market_price'] = (string)$goods_list[$k]['market_price'];
                }

                if ($sku_list) {
                    foreach ($sku_list as $key => $val) {
                        $sku_format_list[$key]['id'] = $val['id'];
                        $sku_format_list[$key]['user_id'] = $val['user_id'];
                        $sku_format_list[$key]['sku_id'] = $val['sku_id'];
                        $sku_format_list[$key]['store_id'] = $val['store_id'];
                        $sku_format_list[$key]['goods_id'] = $val['goods_id'];
                        $sku_format_list[$key]['price'] = (string)$val['price'];

                        if ($is_show_market_price) {
                            $sku_format_list[$key]['market_price'] = (string)$val['market_price'];
                            if ($val['market_price'] != '0.0') {
                                $info['market_price'] = (string)$val['market_price'];
                            }
                        }
                    }
                }

                $info['sku_list'] = $sku_format_list;

                // 获取合伙人店铺的库存
                $agency_sku_list = store_helper::get_store_sku_info($dealers_user_store_info['id'], $v['id']);
                if ($agency_sku_list) {
                    foreach ($agency_sku_list as $key => $val) {
                        // 合伙人改价忽略上架下架问题，门店进货要有下架的问题
                        if ($val['stock'] <= 0 && !$is_show_market_price) {
                            $info['is_sale'] = 0;
                        }
                    }
                }

                $result[] = $info;
            }
        }

        api_helper::return_data(0, 'success', $result, $this->api_stat_id);
    }
}

?>
